// generated 2005/8/8 17:10:19 CST by xihels@163.com
//


#ifndef _MEMBERLIST_HH
#include <gtkmm/treeview.h>
#  define _MEMBERLIST_HH
#include <gtkmm/liststore.h>

#include "CodeConverter.h"
//extern CodeConverter g2u;

#define _ONLINE	    1
#define _AWAY	    2
#define _OFFLINE    3

#define GET_STATUS(A) (QQ_FRIEND_STATUS_ONLINE == (A)) ? _ONLINE : (QQ_FRIEND_STATUS_AWAY == (A) ? _AWAY : _OFFLINE)


#include "ClusterInfo.h"
#include "pixmaps.h"
#include <functional>
using std::unary_function;

class MemberList : public Gtk::TreeView {  
    public:
	typedef ClusterInfo::Members Members;
	MemberList();
	void refresh(const Members& members);
	void refreshStatus(const Members& members);

	int getSelectMemberid();
    private:
	struct MemberColumns : public Gtk::TreeModel::ColumnRecord {
	    MemberColumns() 
	    {
		add(face);
		add(name); 
		add(uid);
		add(status);
	    }
	    Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > face;
	    Gtk::TreeModelColumn<Glib::ustring> name;
	    Gtk::TreeModelColumn<int> uid;
	    Gtk::TreeModelColumn<int> status;
	};
    private:
	// 排序函数
	int on_sort_compare(const Gtk::TreeModel::iterator& a, 
		const Gtk::TreeModel::iterator& b){
	    int result = (*a)[columns.status] - (*b)[columns.status]; 
	    if (0 == result)
		return (*a)[columns.uid] - (*b)[columns.uid];
	    return result;
	}

	template<typename T>
	    struct RefreshStatus : public unary_function<Gtk::TreeRow, void> {

		RefreshStatus(MemberList& _wnd, const T& _list) :
		    wnd(_wnd), list(_list) {}

		void operator () (const Gtk::TreeRow& row);

		MemberList& wnd;
		const T&    list;
	    };

	template<typename T>
	    struct Refresh: public unary_function<Gtk::TreeRow, void> {

		Refresh(MemberList& _wnd, const T& _list) :
		    wnd(_wnd), list(_list) {}

		void operator () (const Gtk::TreeRow& row);

		MemberList& wnd;
		const T&    list;
	    };


	template<typename T>
	    struct AddMembers : public unary_function<typename T::value_type, void> {

		AddMembers(MemberList& _wnd) :
		    wnd(_wnd){}

		void operator () (typename T::value_type value);
		MemberList&	    wnd;
	    };

    private:
	int sortCompare( const Gtk::TreeModel::iterator& lhs,
		const Gtk::TreeModel::iterator& rhs)
	{
	    return (*lhs)[columns.status] < (*rhs)[columns.status];
	}
	MemberColumns columns;
	Glib::RefPtr<Gtk::ListStore> refListStore;
};

    template<typename T>
inline void MemberList::RefreshStatus<T>::operator () (const Gtk::TreeRow& row)
{
    typename T::value_type value = 
	list.getElement(row[wnd.columns.uid]);

    if ((NULL != value) && 
	    (row[wnd.columns.status] != 
	    GET_STATUS(value->getStatus()))) {

	row[wnd.columns.face] = 
	    getPix22(value->getFace(),
		    value->getStatus());

	row[wnd.columns.status] = 
	    GET_STATUS(value->getStatus());

	value->isShowed(true);
    }
}

    template<typename T>
inline void MemberList::Refresh<T>::operator () (const Gtk::TreeRow& row)
{
    typename T::value_type value = 
	list.getElement(row[wnd.columns.uid]);

    if (NULL != value) {

	row[wnd.columns.face] = 
	    getPix22(value->getFace(),
		    value->getStatus());

	row[wnd.columns.name] = value->getNickname();


	row[wnd.columns.status] = 
	    GET_STATUS(value->getStatus());

	value->isShowed(true);
    }
}

    template<typename T>
inline void MemberList::AddMembers<T>::operator () (typename T::value_type value)
{
    if (!value->isShowed()) {
	Gtk::TreeModel::iterator treeiter = 
	    wnd.refListStore->append();

	(*treeiter)[wnd.columns.face] = 
	    getPix22(value->getFace(),
		    value->getStatus());
		    
	(*treeiter)[wnd.columns.name] = value->getNickname();

	(*treeiter)[wnd.columns.uid]  = 
	    value->getUid();

	(*treeiter)[wnd.columns.status] = 
	    GET_STATUS(value->getStatus());

	value->isShowed(true);


    }
}

#endif

